import axios from "axios";
import { put, takeEvery } from "redux-saga/effects";
import {
  getSongUrlFailed,
  getSongUrlSuccess,
  GET_SONG_URL,
  IGetSongUrlAction,
} from "../actions/songurl.action";

function* handleGetSongUrl(action: IGetSongUrlAction) {
  try {
    const { data } = yield axios.get(
      "http://localhost:3001/song/url?id=" + action.payload
    );
    yield put(getSongUrlSuccess(data));
  } catch (error) {
    yield put(getSongUrlFailed(JSON.stringify(error)));
  }
}

export default function* songUrlSaga() {
  yield takeEvery(GET_SONG_URL, handleGetSongUrl);
}
